package test;

import java.util.Stack;

public class test1 {
    //牛客 JZ31 栈的压入和弹出顺序
    public static void main(String[] args) {
        int[] pushV = {2, 1, 0};
        int[] popV = {1, 2, 0};
        System.out.println(IsPopOrder(pushV, popV));
    }

    public static boolean IsPopOrder(int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        int j = 0;
        for (; i < pushV.length; i++) {
            //1.先把元素压入栈
            stack.push(pushV[i]);
            //2.在进行比较
            while (!stack.isEmpty() && stack.peek() == popV[j]) {
                //3.相等弹出并j++
                stack.pop();
                j++;
            }

        }
        //这里后面的是多余的
        //此时pushv已遍历完,检查j是否也遍历完
//        if (j == popV.length && stack.isEmpty()) {
//            return true;
//        }
//
//        while (j < popV.length) {
//            if (popV[j] == stack.peek()) {
//                stack.pop();
//                j++;
//            } else {
//                return false;
//            }
//        }

        return stack.isEmpty();

    }
}
